package com.ruoyi.durian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.enums.DurianRedisKeysType;
import com.ruoyi.durian.domain.LlCoupon;
import com.ruoyi.durian.domain.LlCouponExchange;
import com.ruoyi.durian.domain.vo.CouponGrantVo;
import com.ruoyi.durian.mapper.LlCouponExchangeMapper;
import com.ruoyi.durian.service.LlCouponService;
import com.ruoyi.durian.mapper.LlCouponMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 * @description 针对表【ll_coupon(优惠券模块-优惠券表)】的数据库操作Service实现
 * @createDate 2024-07-26 16:17:15
 */
@Service
public class LlCouponServiceImpl extends ServiceImpl<LlCouponMapper, LlCoupon>
        implements LlCouponService {

    @Resource
    private LlCouponMapper llCouponMapper;

    @Resource
    private LlCouponExchangeMapper llCouponExchangeMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 添加优惠券
     *
     * @param llCoupon
     * @return
     */
    @Override
    @Transactional
    public AjaxResult saveOrUpdateCoupon(LlCoupon llCoupon) {
        if (llCoupon == null) {
            return AjaxResult.error("优惠券信息不能为空");
        }
        String type = llCouponMapper.selectCouponType(llCoupon.getType());
        if ("折扣".equals(type)) {
            BigDecimal decimal = new BigDecimal("100");
            llCoupon.setSaleMoney(decimal.subtract(BigDecimal.valueOf(llCoupon.getSale())));
        }
        if ("满减".equals(type)) {
            llCoupon.setSaleMoney(llCoupon.getMin());
        }

        if (llCoupon.getId() == null) {
            llCoupon.setCreateTime(new Date());
        }
        llCoupon.setUpdateTime(new Date());
        llCoupon.setIsDelete(Long.valueOf(0));
        llCoupon.setStatus(Long.valueOf(0));
        saveOrUpdate(llCoupon);
        return AjaxResult.success();
    }

    /**
     * 删除优惠券
     *
     * @return
     */
    @Override
    @Transactional
    public AjaxResult delCoupon(Integer id) {
        if (id == null) {
            return AjaxResult.error("优惠券id不能为空");
        }
        LlCoupon llCoupon = llCouponMapper.selectById(id);

        if (llCoupon.getStatus() == 0) {
            return AjaxResult.error("上架的优惠券不能删除");
        }
        llCouponMapper.deleteById(id);
        return AjaxResult.success();
    }

    /**
     * 发放优惠券
     *
     * @param couponGrantVo
     * @return
     */
    @Override
    public AjaxResult grantCoupon(CouponGrantVo couponGrantVo) {
        if (couponGrantVo.getNum() == null || couponGrantVo.getNum() <= 0
                || couponGrantVo.getCouponId() == null) {
            return AjaxResult.error("优惠券数量不能为空");
        }

        LambdaQueryWrapper<LlCouponExchange> qw = new LambdaQueryWrapper<>();
        qw.eq(LlCouponExchange::getCouponCode, couponGrantVo.getCode());
        LlCouponExchange coupon = llCouponExchangeMapper.selectOne(qw);
        if (coupon != null) {
            return AjaxResult.error("优惠券已使用");
        }

        Integer userId = 1204;

        LlCouponExchange exchange = new LlCouponExchange();
        exchange.setCouponId(couponGrantVo.getCouponId());
        exchange.setCouponCode(couponGrantVo.getCode());
        exchange.setUserId(userId);
        exchange.setRemainderNum(couponGrantVo.getNum());
        exchange.setAllNum(couponGrantVo.getNum());
        exchange.setStartTime(new Date());
        exchange.setEndTime(couponGrantVo.getEndTime());
        exchange.setCreateTime(new Date());
        exchange.setUpdateTime(new Date());
        exchange.setIsDelete(0);
        llCouponExchangeMapper.insert(exchange);

        stringRedisTemplate.opsForValue().set(DurianRedisKeysType.COUPON.getKey() + couponGrantVo.getCode()
                , couponGrantVo.getNum().toString()
                , Duration.ofMillis(
                        couponGrantVo.getEndTime().getTime() - System.currentTimeMillis()));
        return AjaxResult.success();
    }

    @Override
    public List<LlCoupon> selectList(Integer status) {
        List<LlCoupon> llCardPackageList = llCouponMapper.selectLists(status);
        return llCardPackageList;    }
}




